package com.cqcvc.controller;


import com.alibaba.fastjson.JSON;
import com.cqcvc.config.Result;
import com.cqcvc.entity.RolePermission;
import com.cqcvc.service.RolePermissionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Autowired
    private RolePermissionService rolePermissionService;

    @GetMapping("/select")
    @ApiOperation(value = "根据RoleId查询所有权限信息")
    public String selectRolePermission(Integer rid){
        Map<String,Object> map=new HashMap<>();
        List<RolePermission> list=rolePermissionService.selectByRoleId(rid);
        list.remove(0);
        map.put("list",list);
        return JSON.toJSONString(new Result().setCode(200).setData(map));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存角色分配的权限信息")
    public String saveRolePermission(Integer rid,@RequestBody Integer[] permId){
        rolePermissionService.saveRolePermission(rid,permId);
        return JSON.toJSONString(new Result().setCode(200).setMessage("角色权限分配成功！"));
    }

}
